package com.example.administrator.androidros.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import com.mob.MobSDK;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

/**
 * Created by HuHaiChao on 2017/12/23.
 * <p>
 * Mob 的短信工具
 */

public class MobSMSUtil {
    private static final String TAG = "MobSMSUtil";
    private Context context;
    private MobResultCallBack mobResultCallBack;

    public interface MobResultCallBack {
        void onSendSuccess();

        void onValidateSuccess();

        void onValidateFailed();
    }

    public MobSMSUtil(Context context, MobResultCallBack mobResultCallBack) {
        this.context = context;
        this.mobResultCallBack = mobResultCallBack;
        init();
    }

    private void init() {
        MobSDK.init(context, "233de7904f1ab", "378c748aff5b39d25e3424078d226e7b");
        //注册短信回调
        SMSSDK.registerEventHandler(new EventHandler() {
            @Override
            public void afterEvent(int event, int result, Object data) {
                Handler handler = new Handler(Looper.getMainLooper()){
                    @Override
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);
                        switch (msg.what){
                                case SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE:
                                    //提交验证码成功
                                    mobResultCallBack.onValidateSuccess();
                                    break;
                                case SMSSDK.EVENT_GET_VERIFICATION_CODE:
                                    //获取验证码成功
                                    mobResultCallBack.onSendSuccess();
                                    break;
                                case SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES:
                                    //返回支持发送验证码的国家列表
                                    break;
                                default:
                                    //验证码错误
                                    mobResultCallBack.onValidateFailed();
                                    break;
                            }
                        }
                };
                Message message = new Message();
                if (result == SMSSDK.RESULT_COMPLETE) {
                    message.what = event;
                    message.obj = data;
                    handler.sendMessage(message);
                } else {
                    String json = ((Throwable) data).toString();
                    message.what = -1;
                    message.obj = data;
                    handler.sendMessage(message);
                }
            }
        });
    }

    //获取验证码
    public void getVerificationCode(String telephone){
        SMSSDK.getVerificationCode("86", telephone);
    }

    //验证验证码
    public void validate(String telephone,String captcha){
        SMSSDK.submitVerificationCode("86", telephone, captcha);
    }

    //注销回调接口
    public void destroy(){
        SMSSDK.unregisterAllEventHandler();
    }
}
